{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "strategic-timer",
   "metadata": {
    "papermill": {
     "duration": 0.008041,
     "end_time": "2021-04-19T19:43:17.398618",
     "exception": false,
     "start_time": "2021-04-19T19:43:17.390577",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# A Simple Example of Properties of IV estimator when Instruments are Weak"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "unknown-envelope",
   "metadata": {
    "papermill": {
     "duration": 0.007071,
     "end_time": "2021-04-19T19:43:17.412999",
     "exception": false,
     "start_time": "2021-04-19T19:43:17.405928",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "Simulation Design"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "wanted-validation",
   "metadata": {
    "papermill": {
     "duration": 0.269527,
     "end_time": "2021-04-19T19:43:17.689888",
     "exception": false,
     "start_time": "2021-04-19T19:43:17.420361",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Simulation Design\n",
    "\n",
    "library(hdm)\n",
    "set.seed(1)\n",
    "B= 10000 # trials\n",
    "IVEst = rep(0, B)\n",
    "n=100\n",
    "beta = .25   # .2 weak IV\n",
    "#beta = 1   #   1 strong IV\n",
    "\n",
    "\n",
    "U =  rnorm(n)  \n",
    "Z = rnorm(n)  #generate instrument\n",
    "D = beta*Z + U  #generate endogenougs variable\n",
    "Y =  D+ U  # the true causal effect is 1\n",
    "\n",
    "\n",
    "summary(lm(D~Z))  # first stage is very weak here\n",
    "\n",
    "summary(tsls(x=NULL, d=D, y=Y, z=Z))  #\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "inner-peninsula",
   "metadata": {
    "papermill": {
     "duration": 0.01419,
     "end_time": "2021-04-19T19:43:17.719759",
     "exception": false,
     "start_time": "2021-04-19T19:43:17.705569",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "Note that the instrument is weak here (contolled by $\\beta$) -- the t-stat is less than 4."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "productive-configuration",
   "metadata": {
    "papermill": {
     "duration": 0.014297,
     "end_time": "2021-04-19T19:43:17.748125",
     "exception": false,
     "start_time": "2021-04-19T19:43:17.733828",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# Run 1000 trials to evaluate distribution of the IV estimator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "narrow-boxing",
   "metadata": {
    "papermill": {
     "duration": 2.929023,
     "end_time": "2021-04-19T19:43:20.691148",
     "exception": false,
     "start_time": "2021-04-19T19:43:17.762125",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Simulation Design\n",
    "\n",
    "set.seed(1)\n",
    "B= 10000 # trials\n",
    "IVEst = rep(0, B)\n",
    "\n",
    "for(i in 1:B){\n",
    "U =  rnorm(n)  \n",
    "Z = rnorm(n)  #generate instrument\n",
    "D = beta*Z + U  #generate endogenougs variable\n",
    "Y =  D+ U  # the true causal effect is 1\n",
    "IVEst[i] = coef(tsls(x=NULL, d=D, y=Y, z=Z))[1,1]\n",
    "}\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "functional-connecticut",
   "metadata": {
    "papermill": {
     "duration": 0.008622,
     "end_time": "2021-04-19T19:43:20.714062",
     "exception": false,
     "start_time": "2021-04-19T19:43:20.705440",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# Plot the Actual Distribution against the Normal Approximation (based on Strong Instrument Assumption)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "desperate-standing",
   "metadata": {
    "papermill": {
     "duration": 0.41022,
     "end_time": "2021-04-19T19:43:21.132378",
     "exception": false,
     "start_time": "2021-04-19T19:43:20.722158",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "plot(density(IVEst-1, n=1000, from=-5, to=5),col=4, xlim= c(-5, 5),  \n",
    "     xlab= \"IV Estimator -True Effect\", main=\"Actual Distribution vs Gaussian\")\n",
    "\n",
    "val=seq(-5, 5, by=.05)\n",
    "var = (1/beta^2)*(1/100) # theoretical variance of IV\n",
    "sd = sqrt(var)\n",
    "lines(val, dnorm(val, sd=sd), col=2, lty=2)\n",
    "\n",
    "rejection.frequency = sum(( abs(IVEst-1)/sd > 1.96))/B\n",
    "\n",
    "cat(c(\"Rejection Frequency is \", rejection.frequency, \" while we expect it to be .05\"))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "different-sampling",
   "metadata": {
    "papermill": {
     "duration": 0.010583,
     "end_time": "2021-04-19T19:43:21.154861",
     "exception": false,
     "start_time": "2021-04-19T19:43:21.144278",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# Some Help Functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "beneficial-junior",
   "metadata": {
    "papermill": {
     "duration": 0.238002,
     "end_time": "2021-04-19T19:43:21.404170",
     "exception": false,
     "start_time": "2021-04-19T19:43:21.166168",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "help(tsls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "hearing-tutorial",
   "metadata": {
    "papermill": {
     "duration": 0.184318,
     "end_time": "2021-04-19T19:43:21.599524",
     "exception": false,
     "start_time": "2021-04-19T19:43:21.415206",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "help(density)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "R",
   "language": "R",
   "name": "ir"
  },
  "language_info": {
   "codemirror_mode": "r",
   "file_extension": ".r",
   "mimetype": "text/x-r-source",
   "name": "R",
   "pygments_lexer": "r",
   "version": "3.6.3"
  },
  "papermill": {
   "default_parameters": {},
   "duration": 7.855737,
   "end_time": "2021-04-19T19:43:21.720299",
   "environment_variables": {},
   "exception": null,
   "input_path": "__notebook__.ipynb",
   "output_path": "__notebook__.ipynb",
   "parameters": {},
   "start_time": "2021-04-19T19:43:13.864562",
   "version": "2.3.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
